//
//  BaseViewController.m
//  LFCWarmSystem
//
//  Created by Kill on 16/4/28.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<MFMessageComposeViewControllerDelegate>{

}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:NSLocalizedString(@"WarmSystem_Sample",@"报警系统") leftBarItemsTitle:@[NSLocalizedString(@"Cancel_Sample", @"取消")] rightBarItemsTitle:@[NSLocalizedString(@"Complete_Sample", @"完成")]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
    [self.view addGestureRecognizer:tap];
    _lblWaiting = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*0.8, 44)];
    _lblWaiting.center = self.view.center;
    _lblWaiting.text = NSLocalizedString(@"PleaseWaiting...", @"请等待...");
    _lblWaiting.backgroundColor = [UIColor grayColor];
    // Do any additional setup after loading the view.
}
#pragma mark - keyboardHight
-(void)viewWillAppear:(BOOL)animated {
    [self registerForKeyboardNotifications];
}

-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)backAction:(id)sender{
    [self resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:^{}];
}
-(void)rightAction:(id)sender{
    [self showSMSPicker:@"9"];
    NSLog(@"RightAction");
}

#pragma mark-Send Message
-(void)showSMSPicker:(NSString *)contect{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            picker.recipients = [NSArray arrayWithObjects:_modelPhone.phoneNumber, nil];
            picker.messageComposeDelegate = self;
            picker.body = contect;
            [self presentViewController:picker animated:YES completion:^{
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"DeviceCantMSG", @"设备没有短信功能")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"VersionProblem", @"iOS版本过低,iOS4.0以上才支持程序内发送短信") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
        [alert show];
    }
}

#pragma mark-MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    
    switch (result)
    {
        case MessageComposeResultCancelled:
            NSLog(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
            NSLog(@"Result: SMS sent");
            break;
        case MessageComposeResultFailed:{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SendMSGFail", @"短信发送失败")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
            break;
        default:
            NSLog(@"Result: SMS not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }];
}

-(void)hideKeyBoard:(UIGestureRecognizer *)gesture{
    [self.view endEditing:YES];
}


- (void)registerForKeyboardNotifications {
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
}

//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification {

}

//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    //do something
}

@end
